﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Data.Common;

namespace Cashier_Service
{
    public class BillPurchasePaymentService
    {
        /// <summary>
        /// 查询采购单付款信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BillPurchasePayment GetBillPurchasePayment(long id)
        {
            BillPurchasePayment purchasePayment = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    purchasePayment = new BillPurchasePayment();
                    purchasePayment.Id = id;
                    purchasePayment = new BillPurchasePaymentDAO().GetBillPurchasePayment(conn, purchasePayment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return purchasePayment;
        }

        /// <summary>
        /// 查询采购单付款信息列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="enterpriseNo"></param>
        /// <param name="storeNo"></param>
        /// <param name="purchaseno"></param>
        /// <param name="supplierno"></param>
        /// <param name="adminUsername"></param>
        /// <param name="ascfield"></param>
        /// <param name="descfield"></param>
        /// <returns></returns>
        public DataTable GetBillPurchasePaymentTable(int pageindex, int pagesize, String enterpriseNo, String storeNo, String purchaseno, String supplierno,
            String adminUsername, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillPurchasePayment purchasePayment = new BillPurchasePayment();
                    purchasePayment.Pageindex = pageindex;
                    purchasePayment.Pagesize = pagesize;
                    purchasePayment.EnterpriseNo = enterpriseNo;
                    purchasePayment.StoreNo = storeNo;
                    purchasePayment.PurchaseNo = purchaseno;
                    purchasePayment.SupplierNo = supplierno;
                    purchasePayment.AdminUsername = adminUsername;
                    purchasePayment.Ascfield = ascfield;
                    purchasePayment.Descfield = descfield;
                    dt = new BillPurchasePaymentDAO().GetBillPurchasePaymentTable(conn, purchasePayment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询采购单付款信息记录数
        /// </summary>
        /// <param name="enterpriseNo"></param>
        /// <param name="storeNo"></param>
        /// <param name="purchaseno"></param>
        /// <param name="supplierno"></param>
        /// <param name="adminUsername"></param>
        /// <returns></returns>
        public long GetBillPurchasePaymentRecord(String enterpriseNo, String storeNo, String purchaseno, String supplierno,
            String adminUsername)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillPurchasePayment purchasePayment = new BillPurchasePayment();
                    purchasePayment.EnterpriseNo = enterpriseNo;
                    purchasePayment.StoreNo = storeNo;
                    purchasePayment.PurchaseNo = purchaseno;
                    purchasePayment.SupplierNo = supplierno;
                    purchasePayment.AdminUsername = adminUsername;
                    record = new BillPurchasePaymentDAO().GetBillPurchasePaymentRecord(conn, purchasePayment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 新增采购单付款信息
        /// </summary>
        /// <param name="purchasePayment"></param>
        /// <returns></returns>
        public long InsertBillPurchasePayment(BillPurchasePayment purchasePayment)
        {
            long id = 0;
            using (DbConnection conn = ConnectionFactory.GetDbConnection())
            {
                DbTransaction trans = null;
                try
                {
                    conn.Open();
                    trans = conn.BeginTransaction();
                    id = new BillPurchasePaymentDAO().InsertBillPurchasePayment(conn, purchasePayment);
                    if (id > 0)
                    {
                        int flag = new BillPurchaseService().UpdateDebtAmount(conn, purchasePayment.PurchaseNo, purchasePayment.RemainAmount);
                        if (flag > 0)
                            trans.Commit();
                        else
                            throw new Exception("update billPurchase debt_amount error!");
                    }
                }
                catch (Exception ex)
                {
                    if (trans != null)
                        trans.Rollback();
                    throw ex;
                }
                finally
                {
                    trans = null;
                }
            }
            return id;
        }

        /// <summary>
        /// 更新采购单付款信息
        /// </summary>
        /// <param name="purchasePayment"></param>
        /// <returns></returns>
        public int UpdateBillPurchasePayment(BillPurchasePayment purchasePayment)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new BillPurchasePaymentDAO().UpdateBillPurchasePayment(conn, purchasePayment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除采购单付款信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteBillPurchasePayment(String ids)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        String[] arr = ids.Split(new Char[] { ',' });
                        BillPurchasePaymentDAO paymentDAO = new BillPurchasePaymentDAO();
                        BillPurchasePayment payment = new BillPurchasePayment();
                        foreach (String lopid in arr)
                        {
                            payment.Id = Int64.Parse(lopid);
                            count += paymentDAO.DeleteBillPurchasePayment(conn, payment);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }
    }
}
